package com.eleven.mall.service.impl;

import com.eleven.mall.customexception.BusinessException;
import com.eleven.mall.mapper.CategoryMapper;
import com.eleven.mall.pojo.CategoryOut;
import com.eleven.mall.pojo.ExType;
import com.eleven.mall.pojo.entity.Category;
import com.eleven.mall.pojo.entity.CategoryExample;
import com.eleven.mall.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class CategoryServiceImpl implements CategoryService {
    CategoryMapper categoryMapper;

    //    得到订单列表
   /* @Override
    public List<CategoryOut> getCatList(Integer type){

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andCatLevelEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

//        PageHelper.startPage(Integer.parseInt(pagenum), pagesize);


        //List<Category> categories = categoryMapper.selectByExample(categoryExample);

        if (categories == null || categories.isEmpty()){
            throw new BusinessException("managerList not found", ExType.BussRecordNotFound);
        }

        ArrayList<CategoryOut> cateOuts = new ArrayList<>();
        //4、遍历 categories 存到categoryOut集合中作为响应的用户列表数据
        for(Category c : categories) {
//            List<CategoryOut> category = new List<>();
            //categoryOuts.setCat_id(c.getCatId());
            CategoryOut categoryOut = new CategoryOut();
            System.out.println(c);
            categoryOut.setCat_id(c.getCatId());
            categoryOut.setCat_name(c.getCatName());
            categoryOut.setCat_level(c.getCatLevel());
            categoryOut.setCat_pid(c.getCatPid());
            if (c.getCatDeleted()==1){
                categoryOut.setCat_deleted(false);
            }else if(c.getCatDeleted()==0){
                categoryOut.setCat_deleted(true);
            }
            categoryOut.setChildren(getCategoryOutByL1(c.getCatId()));
            cateOuts.add(categoryOut);
        }

        return cateOuts;
    }*/

    /*private List<CategoryOut> getCategoryOutByL1(Integer catId) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andCatLevelEqualTo(1);
        List<Category> categoryList = categoryMapper.selectSecond(catId);
        ArrayList<CategoryOut> cateOuts = new ArrayList<>();
        for (Category c : categoryList) {
            CategoryOut categoryOut = new CategoryOut();
            categoryOut.setCat_id(c.getCatId());
            categoryOut.setCat_name(c.getCatName());
            categoryOut.setCat_level(c.getCatLevel());
            categoryOut.setCat_pid(c.getCatPid());
            if (c.getCatDeleted() == 1) {
                categoryOut.setCat_deleted(false);
            } else if (c.getCatDeleted() == 0) {
                categoryOut.setCat_deleted(true);
            }
            //categoryOut.setChildren(getCategoryOutByL2());
            cateOuts.add(categoryOut);
        }
        return cateOuts;
    }*/

    @Override
    public List<CategoryOut> getCatList(Integer type) {
        Integer level = 0;
        if (type<0){
            throw new BusinessException("type不存在" + type, ExType.BussInvalidNum);
        }else if(type==2){
            level = 1;
        }else if(type==1){
            level = 2;
        }
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andCatLevelEqualTo(level);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<CategoryOut> cateOut = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryOut categoryOut = new CategoryOut();
            categoryOut.setCat_id(category.getCatId());
            categoryOut.setCat_name(category.getCatName());
            categoryOut.setCat_deleted(category.getCatDeleted());
            categoryOut.setCat_pid(category.getCatPid());
            categoryOut.setCat_level(category.getCatLevel());

            if(level<2){
                CategoryExample categoryExample2 = new CategoryExample();
                categoryExample2.createCriteria().andCatLevelEqualTo(++level).andCatPidEqualTo(category.getCatId());
                List<Category> cateChild = categoryMapper.selectByExample(categoryExample2);
                System.out.println("========cateChild: "+cateChild);
                List<CategoryOut> child = getChild(cateChild);
                categoryOut.setChildren(child);
            }
            cateOut.add(categoryOut);
        }

        return cateOut;
    }

    private List<CategoryOut> getChild(List<Category> cateChild){
        List<CategoryOut> chids = new ArrayList<>();
        for (Category category : cateChild) {
            CategoryOut categoryOut2 = new CategoryOut();
            categoryOut2.setCat_id(category.getCatId());
            categoryOut2.setCat_name(category.getCatName());
            categoryOut2.setCat_deleted(category.getCatDeleted());
            categoryOut2.setCat_pid(category.getCatPid());
            categoryOut2.setCat_level(category.getCatLevel());

            CategoryExample categoryExample2 = new CategoryExample();
            categoryExample2.createCriteria().andCatLevelEqualTo(2).andCatPidEqualTo(category.getCatId());
            List<Category> categories = categoryMapper.selectByExample(categoryExample2);

            List<CategoryOut> categoryOuts = new ArrayList<>();
            for (Category category1 : categories) {
                CategoryOut categoryOut3 = new CategoryOut();
                categoryOut3.setCat_id(category1.getCatId());
                categoryOut3.setCat_name(category1.getCatName());
                categoryOut3.setCat_deleted(category.getCatDeleted());
                categoryOut3.setCat_pid(category1.getCatPid());
                categoryOut3.setCat_level(category1.getCatLevel());
                categoryOuts.add(categoryOut3);
            }
            categoryOut2.setChildren(categoryOuts);
            chids.add(categoryOut2);
        }
        return chids;
    }

    /*private List<CategoryOut> getCategoryOutByL2(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andCatLevelEqualTo(2);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        ArrayList<CategoryOut> cateOuts = new ArrayList<>();
        for (Category c : categoryList) {
            CategoryOut categoryOut = new CategoryOut();
            categoryOut.setCat_id(c.getCatId());
            categoryOut.setCat_name(c.getCatName());
            categoryOut.setCat_level(c.getCatLevel());
            categoryOut.setCat_pid(c.getCatPid());
            if (c.getCatDeleted() == 1){
                categoryOut.setCat_deleted(false);
            }else if(c.getCatDeleted() == 0){
                categoryOut.setCat_deleted(true);
            }
            cateOuts.add(categoryOut);
        }
        return cateOuts;
    }*/
}
